package org.cyb.demo.controller;

import javax.servlet.http.HttpSession;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.cyb.demo.common.HrmConstants;
import org.cyb.demo.domain.User;
import org.cyb.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {

	@Autowired
	@Qualifier("userService")
	private UserService userService;

	//登录
	@RequestMapping(value="/login")
	 public ModelAndView login(
	 		User user,
			HttpSession session,
			ModelAndView modelAndView
	){
		// 调用业务逻辑组件判断用户是否可以登录
		User correctUser = userService.login(user);
		if(correctUser != null){
			// 将用户保存到HttpSession当中
			session.setAttribute(HrmConstants.USER_SESSION, correctUser);

			int admin = 1;
			if(correctUser.getUserStatus() == admin)
			{
				//userStatus为“1”时，跳转到管理员主页
				modelAndView.setViewName("admin/blank");
			}
			else
			{
				//userStatus为“2”时，跳转到普通用户主页
				modelAndView.setViewName("student/blank");
			}
		}else{
			// 设置登录失败提示信息
			modelAndView.addObject("message", "登录名或密码错误!请重新输入");
			// 服务器内部跳转到登录页面
			modelAndView.setViewName("forward:/login.jsp");
		}
		return modelAndView;
	}

	//跳转到添加学生页面
	@RequestMapping(value = "/addStudentPage")
	public ModelAndView addStudentpage(ModelAndView modelAndView)
	{
		modelAndView.setViewName("admin/StudentCreate");
		return modelAndView;
	}

	//跳转到学生信息页面
	@RequestMapping(value = "/studentShowPage")
	public ModelAndView studentShowPage(ModelAndView modelAndView)
	{
		List<User> userList = userService.findAll();
		modelAndView.addObject("userList",userList);
		modelAndView.setViewName("admin/StudentShow");
		return modelAndView;
	}

	//添加学生
	@RequestMapping(value = "/addStudentForm")
	public ModelAndView addStudent(
	        //此时前端的值已经传到了user
			User user,
			ModelAndView modelAndView
	)
	{
		userService.add(user);
		modelAndView.addObject("message","添加成功");
		modelAndView.setViewName("admin/StudentShow");
		return modelAndView;
	}

	//根据账号搜索学生
	@RequestMapping(value = "/selectByAccount")
	public ModelAndView selectByAccount(
			User user,
			ModelAndView modelAndView
	)
	{
		List<User> userList = userService.findByAccount(user);
		modelAndView.addObject("userList",userList);
		modelAndView.setViewName("admin/StudentShow");
		return modelAndView;
	}

	//根据姓名搜索学生
	@RequestMapping(value = "/selectByName")
	public ModelAndView selectByName(
			User user,
			ModelAndView modelAndView
	)
	{
		List<User> userList = userService.findByName(user);
		modelAndView.addObject("userList",userList);
		modelAndView.setViewName("admin/StudentShow");
		return modelAndView;
	}

	//跳转到个人信息页面
	@RequestMapping(value = "/userInfo")
	public ModelAndView userInfo(
			HttpSession session,
			ModelAndView modelAndView
	)
	{
		modelAndView.addObject("user",session.getAttribute(HrmConstants.USER_SESSION));
		modelAndView.setViewName("student/studentInformation");
		return modelAndView;
	}

	//跳转到修改密码页面
	@RequestMapping(value = "/modifyPasswordPage")
	public ModelAndView modifyPasswordPage(
			ModelAndView modelAndView
	)
	{
		modelAndView.setViewName("student/modifyPassword");
		return modelAndView;
	}

	//学生用户修改信息
	@RequestMapping(value = "/modifyPassword")
	public ModelAndView modifyPassword(
			//取参数
			@RequestParam("orignalPassword")String orignalPassword,
			@RequestParam("newPassword")String newPassword,

			HttpSession session,
			ModelAndView modelAndView
	)
	{
		User sessionUser = (User) session.getAttribute(HrmConstants.USER_SESSION);
		if (orignalPassword.equals(sessionUser.getUserPassword())) {
			userService.remove(sessionUser);
			sessionUser.setUserPassword(newPassword);
			userService.add(sessionUser);
			session.setAttribute(HrmConstants.USER_SESSION,sessionUser);
			modelAndView.addObject("user",sessionUser);
			modelAndView.setViewName("student/studentInformation");
		}
		else{
			modelAndView.addObject("message","原密码输入错误");
			modelAndView.setViewName("student/modifyPassword");
		}
		return modelAndView;
	}

	//跳转到homepage
	@RequestMapping(value = "/homepage")
	public ModelAndView homepage(
			ModelAndView modelAndView
	)
	{
		modelAndView.setViewName("student/homepage");
		return modelAndView;
	}

	//学生信息页面：删除学生用户并跳转回学生信息页面
	@RequestMapping(value = "/deleteByAccount")
	public ModelAndView deleteByAccount(
			User user,
			ModelAndView modelAndView
	){
		userService.delete(user);
		modelAndView.setViewName("admin/StudentShow");
		System.out.println();
		return modelAndView;
	}


}


